package org.example;


import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.core.config.GlobalConfig;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.example.entity.User;
import org.example.mapper.UserMapper;
import org.springframework.jdbc.datasource.SimpleDriverDataSource;

import javax.sql.DataSource;
import java.io.Reader;
import java.security.SecureRandom;


public class NoSpring {

    private static SqlSessionFactory sqlSessionFactory;

    static {
        try {
            sqlSessionFactory = initSqlSessionFactory();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    public static void main(String[] args) {

        //初始化
        try (SqlSession session = sqlSessionFactory.openSession(true)) {
            //创建mapper对象
            UserMapper mapper = session.getMapper(UserMapper.class);
            System.out.printf(""+mapper);
            User user = new User();
            user.setId(generateSecureRandomInt());
            user.setPassword(generateSecureRandomInt()+"'");
            user.setUsername(generateSecureRandomInt()+"'");
            mapper.insert(user);
            User user1 = mapper.selectById(user.getId());
            System.out.printf(""+user1);
        }
    }

    //工厂方法
    public static SqlSessionFactory initSqlSessionFactory() throws Exception {
//        DataSource dataSource = dataSource();
//        TransactionFactory transactionFactory = new JdbcTransactionFactory();
//        Environment environment = new Environment("Production", transactionFactory, dataSource);
//        MybatisConfiguration configuration = new MybatisConfiguration();
//        configuration.setEnvironment(environment);
//        //在这里添加Mapper
//        configuration.addMapper(UserMapper.class);
//        configuration.setLogImpl(StdOutImpl.class);
//        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
//        return sqlSessionFactory;
        String resource = "mybatis-config.xml";
        Reader reader = Resources.getResourceAsReader(resource);


        // 设置 MyBatis 的配置文件路径（可选）
        // sessionFactory.setConfigLocation(new ClassPathResource("mybatis-config.xml"));

        // 设置 Mapper XML 文件路径（可选）
        // sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/*.xml"));
        MybatisSqlSessionFactoryBean mybatisSqlSessionFactoryBean = new MybatisSqlSessionFactoryBean();
        mybatisSqlSessionFactoryBean.setDataSource(dataSource());
        MybatisConfiguration mybatisConfiguration = new MybatisConfiguration();
        mybatisConfiguration.getMapperRegistry().addMappers("org.example.mapper");
        // 设置 MyBatis-Plus 的全局配置（可选）
        com.baomidou.mybatisplus.core.config.GlobalConfig globalConfig = new com.baomidou.mybatisplus.core.config.GlobalConfig();
        GlobalConfig.DbConfig dbconfig = new GlobalConfig.DbConfig();
        dbconfig.setDbType(DbType.MYSQL);
        globalConfig.setDbConfig(dbconfig);
        mybatisSqlSessionFactoryBean.setGlobalConfig(globalConfig);
        mybatisSqlSessionFactoryBean.setConfiguration(mybatisConfiguration);
        return mybatisSqlSessionFactoryBean.getObject();

    }

    //连接进行
    public static DataSource dataSource() {
        SimpleDriverDataSource dataSource = new SimpleDriverDataSource();
        dataSource.setDriverClass(com.mysql.jdbc.Driver.class);
        dataSource.setUrl("jdbc:mysql://localhost:3306/test?serverTimezone=GMT%2B8");
        dataSource.setUsername("root");
        dataSource.setPassword("123456");
        return dataSource;
    }

    // 生成一个随机 int 值
    public static int generateSecureRandomInt() {
        SecureRandom secureRandom = new SecureRandom();
        return secureRandom.nextInt();
    }

}